#include <iostream>
#include <cstdio>
using namespace std;
int k;
struct node
{
    int data;
    struct node *left;
    struct node *right;
};
void createT(struct node *tree)
{
    int p;
    cin >> p;
    if (p == 0)
    {
        tree = NULL;
    }
    else
    {
        tree = new node;
        tree->data = p;
        tree->left = NULL;
        tree->right = NULL;
        createT(tree->left);
        createT(tree->right);
    }
}
bool flag = false;
void p(struct node *tree, int deep)
{
    if (tree == NULL)
        return;
    if (deep == k)
    {
        flag = true;
        printf("%d", (*tree).data);
    }
    if (!flag)
    {
        deep += 1;
        p(tree->right, deep);
        if (!flag)
            p(tree->right, deep);
    }
}
int main()
{
    node tree;
    createT(&tree);
    cin >> k;
    p(&tree, 0);
    if (!flag)
        printf("0");
    return 0;
}